#include "lineedit.h"
#include <QMouseEvent>
#include <QApplication>

Lineedit::Lineedit(QWidget *parent) :
    QLineEdit(parent)
{
    setAcceptDrops(true);
    setDragEnabled(true);
}
void Lineedit::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    startPos = event->pos();
    QLineEdit::mousePressEvent(event);
}
void Lineedit::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton)
    {
        int distance = (event->pos() - startPos).manhattanLength();
        if (distance >= QApplication::startDragDistance())
        performDrag();
    }
        QLineEdit::mouseMoveEvent(event);
}
void Lineedit::performDrag()
{
    if (true)
    {
        QMimeData *mimeData = new QMimeData;
        mimeData->setText(text());
        QDrag *drag = new QDrag(this);
        drag->setMimeData(mimeData);
        drag->setPixmap(QPixmap(":/images/person.png"));
        if (drag->exec(Qt::MoveAction) == Qt::MoveAction)
        setText("");
    }
}
void Lineedit::dragEnterEvent(QDragEnterEvent *event)
{
    Lineedit *source =
    qobject_cast<Lineedit *>(event->source());
    if (source && source != this)
    {
        event->setDropAction(Qt::MoveAction);
        event->accept();
    }
}
void Lineedit::dragMoveEvent(QDragMoveEvent *event)
{
    Lineedit *source =
    qobject_cast<Lineedit *>(event->source());
    if (source && source != this)
    {
        event->setDropAction(Qt::MoveAction);
        event->accept();
    }
}
void Lineedit::dropEvent(QDropEvent *event)
{
    Lineedit *source =
    qobject_cast<Lineedit *>(event->source());
    if (source && source != this)
    {
        setText(event->mimeData()->text());
        event->setDropAction(Qt::MoveAction);
        event->accept();
    }
}

